<template>
  <el-tooltip class="item" effect="dark" placement="bottom">
    <div @click="open">
      <svg-icon v-bind="$attrs" icon="change-theme" />
    </div>
    <template #content>
      <div>点击选择颜色</div>
    </template>
  </el-tooltip>

  <!-- 弹出层 -->
  <el-dialog
    :model-value="modelValue"
    title="提示"
    width="400px"
    :before-close="close"
  >
    <div class="select-theme-container">
      <div>请选择主题色 {{ mainColor }}</div>
      <el-color-picker v-model="mainColor" />
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { generateNewStyle, writeStyle } from '@/utils/theme'
import { defineProps, defineEmits, ref } from 'vue'
import { useStore } from 'vuex'
// import color from 'css-color-function'
// import rgbHex from 'rgb-hex'
// console.log(rgbHex(color.convert('color(#ff0000 shade(90%))')))
const store = useStore()
defineProps({
  modelValue: {
    type: Boolean,
    required: true
  }
})
const mainColor = ref(store.getters.mainColor)
// 注册emit事件
const emits = defineEmits(['update:modelValue'])
const close = () => {
  // 关闭
  emits('update:modelValue', false)
}
const open = () => {
  // 打开
  emits('update:modelValue', true)
}
const confirm = async () => {
  const cssText = await generateNewStyle(mainColor.value)
  writeStyle(cssText)
  store.commit('theme/setMainColor', mainColor.value)
  close()
}
</script>

<style lang="scss" scoped>
.select-theme-container {
  @include flex-center;
}
</style>
